package com.chushouya.order.rabbitmq.listener;

import com.general.framework.core.exception.BaseException;
import com.general.framework.core.exception.ViolationException;
import com.general.framework.core.lang.Jsons;
import com.general.framework.core.lang.Strings;
import com.chushouya.common.dto.QueueMessage;
import com.chushouya.order.dto.mq.promote.PromoteMqDTO;
import com.chushouya.order.service.api.PartnerPromoteApiService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 推广标签消息监听器
 * 
 * @author chushouya
 * @date 2025-08-17
 */
@Component
@Slf4j
public class PromoteLabelListener {

    @Resource
    private PartnerPromoteApiService partnerPromoteApiService;

    @Resource
    private ObjectMapper objectMapper;

    /**
     * 监听推广标签处理消息
     */
    @RabbitListener(queuesToDeclare = @Queue(name = "chushouya.promote.label", durable = "true"))
    public void handlePromoteLabelMessage(QueueMessage<?> queueMessage) {
        try {
            log.info("收到推广标签处理消息: messageId={}, data={}", queueMessage.getMessageId(), queueMessage.getData());
            
            // 验证消息数据
            if (queueMessage.getData() == null) {
                log.error("推广标签消息数据为空: messageId={}", queueMessage.getMessageId());
                log.warn("数据为空的消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
                return;
            }
            
            // 解析消息内容
            PromoteMqDTO promoteMqDTO = Jsons.parseObject(Jsons.toJsonString(queueMessage.getData()), PromoteMqDTO.class);
            log.info("开始处理推广标签，订单ID: {}, 推广标签: {}, messageId: {}", 
                    promoteMqDTO.getOrderId(), promoteMqDTO.getPromoteLabel(), queueMessage.getMessageId());
            
            // 处理推广标签逻辑
            processPromoteLabel(promoteMqDTO);
            
            log.info("推广标签处理完成，订单ID: {}, 推广标签: {}, messageId: {}", 
                    promoteMqDTO.getOrderId(), promoteMqDTO.getPromoteLabel(), queueMessage.getMessageId());
            
        } catch (ViolationException e) {
            log.error("推广标签消息验证错误: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId());
            // 验证异常不抛出，避免消息重新入队
            log.warn("验证异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        } catch (BaseException e) {
            log.error("推广标签消息业务异常: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId());
            // 业务异常不抛出，避免消息重新入队
            log.warn("业务异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        } catch (Exception e) {
            log.error("处理推广标签消息异常: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId(), e);
            // 其他异常不抛出，避免消息重新入队
            log.warn("异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        }
    }

    /**
     * 处理推广标签业务逻辑
     * 
     * @param promoteMqDTO 推广消息DTO
     * @return 处理结果
     */
    private void processPromoteLabel(PromoteMqDTO promoteMqDTO) {
        Long orderId = promoteMqDTO.getOrderId();
        String promoteLabel = promoteMqDTO.getPromoteLabel();
        
        // 参数校验
        if (orderId == null || orderId <= 0) {
            log.error("订单ID无效: {}", orderId);
            throw new IllegalArgumentException("订单ID无效");
        }
        
        if (Strings.isEmpty(promoteLabel)) {
            log.error("推广标签为空，订单ID: {}", orderId);
            throw new IllegalArgumentException("推广标签为空");
        }
        
        // 调用推广服务处理业务逻辑
        partnerPromoteApiService.processPartnerPromote(promoteMqDTO);
    }
}
